package org.jeecg.modules.helper;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.MemberModel;
import org.jeecg.common.util.DateUtils;
import org.jeecg.config.app.AppContext;
import org.jeecg.modules.api.member.entity.Member;
import org.jeecg.modules.api.member.service.IMemberService;
import org.jeecg.modules.api.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 会员帮助类
 * @author: Young Jun
 * @date: 2024年09月24日 11:47
 */
@Component
public class MemberHelp {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 获取当前用户信息
     *
     * @return
     */
    public Member getMember() {
        MemberModel memberModel = AppContext.get();
        if (memberModel == null) {
            throw new JeecgBootException("操作失败！无法获取当前用户信息");
        }
        Member member = memberService.getById(memberModel.getId());
        if (member == null) {
            throw new JeecgBootException("操作失败！无法找到当前用户信息");
        } else {
            this.setMemberExtension(member);
            return member;
        }
    }

    public  void setMemberExtension(Member member){
        // 获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));
        String startday = DateUtils.formatDate(cale.getTime(),"yyyy-MM-dd");
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastday = DateUtils.formatDate(cale.getTime(),"yyyy-MM-dd")+" 23:59:59";
        BigDecimal monthAmount= orderMapper.getSumFyAmount(member.getId().toString(),startday,lastday);
        //获取本月推广金额
        member.setCurrentMonthExtension(monthAmount);
        //获取上个月推广金额
        Calendar cale2 = Calendar.getInstance();
        cale2.add(Calendar.MONTH, -1);
        cale2.set(Calendar.DAY_OF_MONTH, cale.getActualMinimum(Calendar.DAY_OF_MONTH));
        String startday2 = DateUtils.formatDate(cale2.getTime(),"yyyy-MM-dd");
        cale2.set(Calendar.DAY_OF_MONTH, cale2.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastday2 = DateUtils.formatDate(cale2.getTime(),"yyyy-MM-dd")+" 23:59:59";
        BigDecimal monthAmount2= orderMapper.getSumFyAmount(member.getId().toString(),startday2,lastday2);
        member.setLastMonthExtension(monthAmount2);
    }
}
